﻿using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Web.UI;
using System.Xml;
using System.Xml.Linq;
using VPLink2013.Framework;
using VPLink2013.Framework.Constants;
using VPLink2013.Framework.Extensions;
using VPLink2014.Data;
using VPLink2014.Data.MesApplis;

namespace VPLink2013.Webcontrols
{
    public class MenuGeneral: WebcontrolVPLink
    {
        public string FilAriane = string.Empty;
        StringBuilder sbFilAriane = new StringBuilder();
        private string xslPath = string.Empty;
        protected override void Render(HtmlTextWriter writer)
        {
            Template = "webcontrol MenuGeneral";

            string strHtml = string.Empty;
            xslPath = pathVP + @"\XSL\MenuGeneral\MenuGeneral.xsl";
            if (FilAriane == "1")
            {
                xslPath = pathVP + @"\XSL\MenuGeneral\FilAriane.xsl";
            }
            try
            {
                if (FilAriane == "1")
                {
                    Template = "webcontrol FilAriane";
                    string strPage = this.Context.Request.Url.AbsolutePath.ToLower();
                    ContextTrace.AddTrace("strPage : " + strPage);

                    if (strPage.Contains(ListNames.NewsDetailName.ToLower()))
                    {
                        sbHtml.Append(GetHtmlNewsDetail2013());
                        base.Render(writer);
                        return;
                    }

                    if (strPage.Contains("accueil") 
                        || strPage.Contains("annonce") 
                        || strPage.Contains("default"))
                    {
                        sbHtml.Append(" ");
                        base.Render(writer);
                        return;
                    }
                }

                string login = VPLink2013XsltTransformHelper.StringAfter(this.Context.User.Identity.Name);
                
                strFileCacheUtilisateur = Path.Combine(pathCache,
                    string.Format(@"Utilisateur\{0}\profil.xml", login));

                string menuPerso = Path.Combine(pathCache, 
                    string.Format(@"Utilisateur\{0}\menu.xml", login));

                if (!FileSerialization.CheckFileCacheExists(strFileCacheUtilisateur, 60 * 12))
                {
                    SetUtilisateurCrt();
                }
                else
                {
                    UtilisateurCrt = FileSerialization.CreateObjFromXML<Utilisateur>(strFileCacheUtilisateur);
                }

                string strXml = string.Empty;
                if (!FileSerialization.CheckFileCacheExists(menuPerso, 60))
                {
                    strXml = ProcessMenuGeneral();
                    FileSerialization.TraceFile(menuPerso, strXml);
                }
                else
                {
                    XDocument xdoc = XDocument.Load(menuPerso);
                    
                    XmlDocument doc = new XmlDocument();
                    doc.Load(menuPerso);
                    XNode menuElement = xdoc.FirstNode;

                    string strPageCrt = GetCurrentPageUrl();
                    XmlNode menuNode = doc.FirstChild;

                    XmlAttribute currentPageAtt = menuNode.Attributes["currentPage"];
                    currentPageAtt.Value = isDefaultPage ? "homePage" : "innerPage";

                    XmlNode previousActiveNode =  menuNode.SelectSingleNode(@"//level1[@class='active']");
                    if (previousActiveNode != null)
                    {
                        previousActiveNode.Attributes.RemoveNamedItem("class");
                    }
                    XmlNode activeNode = menuNode.SelectSingleNode(string.Format(@"//*[@link='{0}']", strPageCrt));
                    
                    if (activeNode != null)
                    {
                        XmlAttribute attrActive = doc.CreateAttribute("class");
                        attrActive.Value = "current";

                        if (!activeNode.Name.Equals("level1", StringComparison.CurrentCultureIgnoreCase))
                        {
                            XmlNode parent = activeNode.SelectSingleNode("..");
                            XmlAttribute attrActive2 = doc.CreateAttribute("class");
                            attrActive2.Value = "currentlevel2";
                            if (parent.Name.Equals("level1", StringComparison.CurrentCultureIgnoreCase))
                            {
                                activeNode.Attributes.Append(attrActive2);
                                parent.Attributes.Append(attrActive);
                            }
                            else
                            {
                                XmlAttribute attrActive3 = doc.CreateAttribute("class");
                                attrActive3.Value = "currentlevel3";
                                activeNode.Attributes.Append(attrActive3);
                                parent.Attributes.Append(attrActive2);
                                parent.SelectSingleNode("..").Attributes.Append(attrActive); 
                            }
                        }
                        else
                        {
                            activeNode.Attributes.Append(attrActive);
                        }
                    }
                    strXml = menuNode.OuterXml;
                }
                strHtml = VPLink2013XsltTransformHelper.GetTransform(strXml, xslPath, login, pathTemp);
            }
            catch (Exception e)
            {
            }
            sbHtml.Append(strHtml);
            base.Render(writer);
        }

        private string ProcessMenuGeneral()
        {
            string strPageCrt = GetCurrentPageUrl();
            strPageCrt = strPageCrt.ToLower();
            ContextTrace.AddTrace("strPageCrt : " + strPageCrt);


            string strXml = string.Empty;

            string fileName = Path.Combine(pathVP, @"XSL\MenuGeneral\MenuGeneral.xml");
            ContextTrace.AddTrace("doc.Load : " + fileName);
            XmlDocument doc = new XmlDocument();
            doc.Load(fileName);
            
            foreach (XmlNode nod1 in doc.ChildNodes)
            {
                ContextTrace.AddTrace(" nod1." + nod1.Name);

                foreach (XmlNode nod2 in nod1.ChildNodes)
                {
                    string strName2;
                    string strLink2;
                    string strInternalOnly2;
                    string strLaPlaineOnly2;
                    string strManagerOnly2;
                    string strDrhOnly2;
                    string strJediOnly2;
                    SetMenuProperties(nod2, out strName2, out strLink2, out strInternalOnly2, out strLaPlaineOnly2, out strManagerOnly2, out strDrhOnly2, out strJediOnly2);

                    ContextTrace.AddTrace("  nod2." + nod2.Name + " => " + strName2
                        + " / strLink2 : " + strLink2
                        + " / strInternalOnly2 : " + strInternalOnly2
                        + " / strLaPlaineOnly2 : " + strLaPlaineOnly2
                        + " / strManagerOnly2 : " + strManagerOnly2
                        + " / strJediOnly2 : " + strJediOnly2
                        + " / strDrhOnly2 : " + strDrhOnly2);

                    XmlAttribute attrNamePiwik2 = doc.CreateAttribute("namepiwik");
                    attrNamePiwik2.Value = strName2.Replace("/", "-").Replace("'", "\\'").Replace("\"", "\\'");
                    nod2.Attributes.Append(attrNamePiwik2);
                    ContextTrace.AddTrace("nod2.attrNamePiwik2 : " + attrNamePiwik2.Value);

                    if ((string.IsNullOrEmpty(UtilisateurCrt.IsManager) && strManagerOnly2 == "true")
                        || (string.IsNullOrEmpty(UtilisateurCrt.IsInterne) && strInternalOnly2 == "true")
                        || (string.IsNullOrEmpty(UtilisateurCrt.IsJedi) && strJediOnly2 == "true")
                        || (string.IsNullOrEmpty(UtilisateurCrt.IsLaPlaine) && strLaPlaineOnly2 == "true")
                        || (string.IsNullOrEmpty(UtilisateurCrt.IsDRH) && strDrhOnly2 == "true")
                        )
                    {
                        XmlAttribute attrDeleted2 = doc.CreateAttribute("suppression");
                        attrDeleted2.Value = "true";
                        nod2.Attributes.Append(attrDeleted2);
                    }
                    else
                    {
                        if ((UtilisateurCrt.IsManager != "1" && strManagerOnly2 == "true")
                            || (UtilisateurCrt.IsInterne != "1" && strInternalOnly2 == "true")
                            || (UtilisateurCrt.IsJedi != "1" && strJediOnly2 == "true")
                            || (UtilisateurCrt.IsLaPlaine != "1" && strLaPlaineOnly2 == "true")
                            || (UtilisateurCrt.IsDRH != "1" && strDrhOnly2 == "true")
                            )
                        {
                            ContextTrace.AddTrace("nod1.RemoveChild");
                            XmlAttribute attrDeleted2 = doc.CreateAttribute("suppression");
                            attrDeleted2.Value = "true";
                            nod2.Attributes.Append(attrDeleted2);
                        }
                        else
                        {
                            if (strLink2.Equals(strPageCrt, StringComparison.CurrentCultureIgnoreCase))
                            {
                                XmlAttribute attrSelected2 = doc.CreateAttribute("class");
                                attrSelected2.Value = "current";
                                nod2.Attributes.Append(attrSelected2);
                                ContextTrace.AddTrace("nod2.current");
                            }

                            foreach (XmlNode nod3 in nod2.ChildNodes)
                            {
                                string strName3 = GetNodeAttribute(nod3, "name");
                                string strLink3 = GetNodeAttribute(nod3, "link");
                                string strInternalOnly3 = GetNodeAttribute(nod3, "internonly");
                                string strLaPlaineOnly3 = GetNodeAttribute(nod3, "laplaineonly");
                                string strManagerOnly3 = GetNodeAttribute(nod3, "manageronly");
                                string strJediOnly3 = GetNodeAttribute(nod3, "jedionly");
                                string strDrhOnly3 = GetNodeAttribute(nod3, "drhonly"); //Marie Pierre Bonnazi seulement
                                ContextTrace.AddTrace("   nod3." + nod3.Name + " => " + strName3
                                    + " / strLink3 : " + strLink3
                                    + " / strInternalOnly3 : " + strInternalOnly3
                                    + " / strLaPlaineOnly3 : " + strLaPlaineOnly3
                                    + " / strManagerOnly3 : " + strManagerOnly3
                                    + " / strJediOnly3 : " + strJediOnly3
                                    + " / strDrhOnly3 : " + strDrhOnly3);

                                XmlAttribute attrNamePiwik3 = doc.CreateAttribute("namepiwik");
                                attrNamePiwik3.Value = strName3.Replace("/", "-").Replace("'", "\\'").Replace("\"", "\\'");
                                nod3.Attributes.Append(attrNamePiwik3);
                                ContextTrace.AddTrace("nod3.attrNamePiwik3 : " + attrNamePiwik3.Value);

                                if ((string.IsNullOrEmpty(UtilisateurCrt.IsManager) && strManagerOnly3 == "true")
                                    || (string.IsNullOrEmpty(UtilisateurCrt.IsInterne) && strInternalOnly3 == "true")
                                    || (string.IsNullOrEmpty(UtilisateurCrt.IsJedi) && strJediOnly3 == "true")
                                    || (string.IsNullOrEmpty(UtilisateurCrt.IsLaPlaine) && strLaPlaineOnly3 == "true")
                                    || (string.IsNullOrEmpty(UtilisateurCrt.IsDRH) && strDrhOnly3 == "true")
                                    )
                                {
                                    XmlAttribute attrDeleted3 = doc.CreateAttribute("suppression");
                                    attrDeleted3.Value = "true";
                                    nod3.Attributes.Append(attrDeleted3);
                                }
                                else
                                {
                                    if ((UtilisateurCrt.IsManager != "1" && strManagerOnly3 == "true")
                                        || (UtilisateurCrt.IsInterne != "1" && strInternalOnly3 == "true")
                                        || (UtilisateurCrt.IsJedi != "1" && strJediOnly3 == "true")
                                        || (UtilisateurCrt.IsLaPlaine != "1" && strLaPlaineOnly3 == "true")
                                        || (UtilisateurCrt.IsDRH != "1" && strDrhOnly3 == "true")
                                    )
                                    {
                                        XmlAttribute attrDeleted3 = doc.CreateAttribute("suppression");
                                        attrDeleted3.Value = "true";
                                        nod3.Attributes.Append(attrDeleted3);
                                        ContextTrace.AddTrace("nod2.RemoveChild");
                                    }
                                    else
                                    {
                                        if (strLink3.Equals(strPageCrt, StringComparison.CurrentCultureIgnoreCase))
                                        {
                                            XmlAttribute attrSelected2 = doc.CreateAttribute("class");
                                            attrSelected2.Value = "current";
                                            nod2.Attributes.Append(attrSelected2);
                                            ContextTrace.AddTrace("nod2.current");

                                            XmlAttribute attrSelected3 = doc.CreateAttribute("class");
                                            attrSelected3.Value = "currentlevel2";
                                            nod3.Attributes.Append(attrSelected3);
                                            ContextTrace.AddTrace("nod3.current");
                                        }

                                        foreach (XmlNode nod4 in nod3.ChildNodes)
                                        {
                                            string strName4 = GetNodeAttribute(nod4, "name");
                                            string strLink4 = GetNodeAttribute(nod4, "link");
                                            string strInternalOnly4 = GetNodeAttribute(nod4, "internonly");
                                            string strJediOnly4 = GetNodeAttribute(nod4, "jedionly");
                                            string strLaPlaineOnly4 = GetNodeAttribute(nod4, "laplaineonly");
                                            string strManagerOnly4 = GetNodeAttribute(nod4, "manageronly");
                                            string strTools4 = GetNodeAttribute(nod4, "tools");
                                            string strDrhOnly4 = GetNodeAttribute(nod4, "drhonly"); //Marie Pierre Bonnazi seulement

                                            XmlAttribute attrNamePiwik4 = doc.CreateAttribute("namepiwik");
                                            attrNamePiwik4.Value = strName4.Replace("/", "-").Replace("'", "\\'").Replace("\"", "\\'");
                                            nod4.Attributes.Append(attrNamePiwik4);
                                            ContextTrace.AddTrace("nod4.attrNamePiwik4 : " + attrNamePiwik4.Value);

                                            if ((string.IsNullOrEmpty(UtilisateurCrt.IsManager) && strManagerOnly4 == "true")
                                                || (string.IsNullOrEmpty(UtilisateurCrt.IsInterne) && strInternalOnly4 == "true")
                                                || (string.IsNullOrEmpty(UtilisateurCrt.IsJedi) && strJediOnly4 == "true")
                                                || (string.IsNullOrEmpty(UtilisateurCrt.IsLaPlaine) && strLaPlaineOnly4 == "true")
                                                || (string.IsNullOrEmpty(UtilisateurCrt.IsDRH) && strDrhOnly4 == "true"))
                                            {
                                                XmlAttribute attrDeleted4 = doc.CreateAttribute("suppression");
                                                attrDeleted4.Value = "true";
                                                nod4.Attributes.Append(attrDeleted4);
                                            }
                                            else
                                            {
                                                if ((UtilisateurCrt.IsManager != "1" && strManagerOnly4 == "true")
                                                    || (UtilisateurCrt.IsInterne != "1" && strInternalOnly4 == "true")
                                                    || (UtilisateurCrt.IsJedi != "1" && strJediOnly4 == "true")
                                                    || (UtilisateurCrt.IsLaPlaine != "1" && strLaPlaineOnly4 == "true")
                                                    || (UtilisateurCrt.IsDRH != "1" && strDrhOnly4 == "true")
                                                    )
                                                {
                                                    XmlAttribute attrDeleted4 = doc.CreateAttribute("suppression");
                                                    attrDeleted4.Value = "true";
                                                    nod4.Attributes.Append(attrDeleted4);
                                                    ContextTrace.AddTrace("nod3.RemoveChild");
                                                }
                                                else
                                                {
                                                    if (strLink4.Equals(strPageCrt, StringComparison.CurrentCultureIgnoreCase))
                                                    {
                                                        XmlAttribute attrSelected2 = doc.CreateAttribute("class");
                                                        attrSelected2.Value = "current";
                                                        nod2.Attributes.Append(attrSelected2);
                                                        ContextTrace.AddTrace("nod2.current");

                                                        if (FilAriane == "1")
                                                        {
                                                            XmlAttribute attrSelected3 = doc.CreateAttribute("class");
                                                            attrSelected3.Value = "currentlevel2";
                                                            nod3.Attributes.Append(attrSelected3);
                                                            ContextTrace.AddTrace("nod3.current");
                                                        }


                                                        XmlAttribute attrSelected4 = doc.CreateAttribute("class");
                                                        attrSelected4.Value = "currentlevel3";
                                                        nod4.Attributes.Append(attrSelected4);
                                                        ContextTrace.AddTrace("nod4.current");
                                                    }
                                                    if (strTools4 == "true")
                                                    {
                                                        XmlAttribute attrLastLink4 = doc.CreateAttribute("lastlink");
                                                        attrLastLink4.Value = "last-link";
                                                        nod4.Attributes.Append(attrLastLink4);
                                                        ContextTrace.AddTrace("nod4.last-link");
                                                    }
                                                    ContextTrace.AddTrace("    nod4." + nod4.Name + " => " + strName4
                                                        + " / strLink4 : " + strLink4
                                                        + " / strInternalOnly4 : " + strInternalOnly4
                                                        + " / strJediOnly4 : " + strJediOnly4
                                                        + " / strManagerOnly4 : " + strManagerOnly4
                                                        + " / strDrhOnly4 : " + strDrhOnly4);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                if (nod1.Name == "menu")
                {
					if( rechercheVersion.Equals("VPLink2014", StringComparison.CurrentCultureIgnoreCase))
					{
						XmlAttribute currentPageAtt = doc.CreateAttribute( "currentPage" );

						currentPageAtt.Value = isDefaultPage ? "homePage" :  "innerPage";
						
                        nod1.Attributes.Append( currentPageAtt );
					}
					if(encartPersoVersion.Equals("VPLink2014", StringComparison.CurrentCultureIgnoreCase))
					{
						//Add My Applications Links
						ContextTrace.AddTrace( "Recherche des Applications" );

                        XmlNode myApplicationsNode = CreateMyApplicationsNode(doc);
                        if (myApplicationsNode != null)
                        {
                            if (myApplicationsNode.ChildNodes.Count > 0)
                            {
                                nod1.AppendChild(myApplicationsNode);
                            }
                        }
					}
					strXml = nod1.OuterXml;
					ContextTrace.AddTrace( "strXml : " + strXml );
				}
            }
            return strXml;
        }

        private string GetCurrentPageUrl()
        {
            string strPageCrt = this.Context.Request.Url.AbsolutePath;
            string strItemID = ParamFrm("ItemID");
            if (!string.IsNullOrEmpty(strItemID))
            {
                strPageCrt += "?ItemID=" + strItemID;
            }
            string strCategorie = ParamFrm("Categorie");
            if (!string.IsNullOrEmpty(strCategorie))
            {
                strPageCrt += "?Categorie=" + strCategorie;
            }
            return strPageCrt;
        }

        private void SetMenuProperties(XmlNode nod2, out string strName2, out string strLink2, out string strInternalOnly2, out string strLaPlaineOnly2, out string strManagerOnly2, out string strDrhOnly2, out string strJediOnly2)
        {
            strName2 = GetNodeAttribute(nod2, "name");
            strLink2 = GetNodeAttribute(nod2, "link");
            strInternalOnly2 = GetNodeAttribute(nod2, "internonly");
            strLaPlaineOnly2 = GetNodeAttribute(nod2, "laplaineonly");
            strManagerOnly2 = GetNodeAttribute(nod2, "manageronly");
            strDrhOnly2 = GetNodeAttribute(nod2, "drhonly"); //Marie Pierre Bonnazi seulement
            strJediOnly2 = GetNodeAttribute(nod2, "jedionly");
        }
        
		private XmlNode CreateMyApplicationsNode(XmlDocument doc)
		{
			XmlNode myApplicationsNode = doc.CreateNode( XmlNodeType.Element, "level1", null );
			XmlAttribute nameAtt = doc.CreateAttribute( "name" );
			nameAtt.Value = "MES APPLIS";
			myApplicationsNode.Attributes.Append( nameAtt );
			try
			{
				SPSecurity.RunWithElevatedPrivileges( delegate()
				{
					using( VPLinkLogConnection db = new VPLinkLogConnection() )
					{
						ContextTrace.AddTrace( "Ouverture de la base réussi" );
						//ContextTrace.AddTrace( string.Format( "Connection String 2 : {0}", db.VplinkEntities.Connection.ConnectionString ) );
                        
						//SPUser currentSPUser = SPControl.GetContextWeb( this.Context ).CurrentUser;
                        string login = UtilisateurCrt.Login;

						int nbApplications = db.VplinkEntities.nbApplicationsForUser( login );
						if( nbApplications > 0 )
						{
							int nbLines = 5;
							if( nbApplications > 20 )
							{
								nbLines = nbApplications / 4 + ( ( nbApplications % 4 == 0 ) ? 0 : 1 );
							}
							XmlNode nodeGestionAppli = CreateNodeLevel3( doc, "Gérer mes applications", "/Pages/GestionMesApplicationsPerso.aspx" );
							XmlAttribute attTools = doc.CreateAttribute( "tools" );
							XmlAttribute attrLastLink = doc.CreateAttribute( "lastlink" );
							attrLastLink.Value = "last-link";
							attTools.Value = "true";
							nodeGestionAppli.Attributes.Append( attTools );
							nodeGestionAppli.Attributes.Append( attrLastLink );
							
							XmlNode node1 = myApplicationsNode.AppendChild( CreateNodeLevel2( doc, string.Empty ) );
							XmlNode node2 = myApplicationsNode.AppendChild( CreateNodeLevel2( doc, string.Empty ) );
							XmlNode node3 = myApplicationsNode.AppendChild( CreateNodeLevel2( doc, string.Empty ) );
							XmlNode node4 = myApplicationsNode.AppendChild( CreateNodeLevel2( doc, string.Empty ) );

							//Only last column will be filled
							if( nbApplications <= nbLines )
							{
								foreach( var app in db.VplinkEntities.GetSelectedApplicationsForUser( login ) )
								{
									node4.AppendChild( CreateNodeLevel3( doc, app.titre, app.url ) );
								}
								node4.AppendChild( nodeGestionAppli );
							}
							else
							{
								int i = 0;
								// 2 last columns will be filled
								if( nbApplications <= nbLines * 2 )
								{
									foreach( var app in db.VplinkEntities.GetSelectedApplicationsForUser( login ) )
									{
										if( i < nbLines )
										{
											node3.AppendChild( CreateNodeLevel3( doc, app.titre, app.url ) );
										}
										else
										{
											node4.AppendChild( CreateNodeLevel3( doc, app.titre, app.url ) );
										}
										i++;
									}
									node3.AppendChild( nodeGestionAppli );
								}
								else
								{
									// 3 last columns will be filled
									if( nbApplications <= nbLines * 3 )
									{
										foreach( var app in db.VplinkEntities.GetSelectedApplicationsForUser( login ) )
										{
											if( i < nbLines )
											{
												node2.AppendChild( CreateNodeLevel3( doc, app.titre, app.url ) );
											}
											else
											{
												if( i < nbLines * 2 )
												{
													node3.AppendChild( CreateNodeLevel3( doc, app.titre, app.url ) );
												}
												else
												{
													node4.AppendChild( CreateNodeLevel3( doc, app.titre, app.url ) );
												}
											}
											i++;
										}
										node2.AppendChild( nodeGestionAppli );
									}
									//all 4 columns will be filled
									else
									{
										foreach( var app in db.VplinkEntities.GetSelectedApplicationsForUser( login ) )
										{
											if( i < nbLines )
											{
												node1.AppendChild( CreateNodeLevel3( doc, app.titre, app.url ) );
											}
											else
											{
												if( i < nbLines * 2 )
												{
													node2.AppendChild( CreateNodeLevel3( doc, app.titre, app.url ) );
												}
												else
												{
													if( i < nbLines * 3 )
													{
														node3.AppendChild( CreateNodeLevel3( doc, app.titre, app.url ) );
													}
													else
													{
														node4.AppendChild( CreateNodeLevel3( doc, app.titre, app.url ) );
													}
												}
											}
											i++;
										}
										node1.AppendChild( nodeGestionAppli );
									}
								}
							}
						}
					}
				} );
			}
			catch( Exception ex )
			{
				ContextTrace.AddTrace( string.Format( @"Exception lors de la recherche des applications:
												{0}", ex.StackTrace ) );
			}
			return myApplicationsNode;
		}
        
		private XmlNode CreateNodeLevel2(XmlDocument doc, string titre)
		{
			XmlNode emptyNode = doc.CreateNode( XmlNodeType.Element, "level2", null );
			XmlAttribute nameAttr = doc.CreateAttribute( "name" );
			nameAttr.Value = titre;
			emptyNode.Attributes.Append( nameAttr );
			return emptyNode;
		}

		private XmlNode CreateNodeLevel3( XmlDocument doc, string titre, string link )
		{
			XmlNode newNode = doc.CreateNode( XmlNodeType.Element, "level3", null );
			XmlAttribute attrName = doc.CreateAttribute( "name" );
			XmlAttribute attrLink = doc.CreateAttribute( "link" );
			attrName.Value = titre;
			attrLink.Value = link;
			newNode.Attributes.Append( attrName );
			newNode.Attributes.Append( attrLink );
			return newNode;
		}
		
		private string GetNodeAttribute( XmlNode nod, string name )
        {
            string strValue = string.Empty;
            try
            {
                strValue = nod.Attributes[name].Value;
                if (string.IsNullOrEmpty(strValue))
                {
                    strValue = "";
                }
            }
            catch (System.Exception) { }
            return strValue;
        }


        #region news detail 2013

        private string GetHtmlNewsDetail2013()
        {
            string strHtml = string.Empty;

            try
            {
                using (SPSite site = new SPSite(siteUrl))
                {
                    using (SPWeb web = site.RootWeb)
                    {
                        SPList lst = web.Lists.TryGetList(ListTitles.NewsVPTitle);
                        if (lst != null)
                        {
                            ProcessNewsVP(lst);
                        }
                    }
                    strHtml = sbFilAriane.ToString();
                }
            }
            catch (System.Exception exc)
            {
                ContextTrace.AddTrace("Erreur GetHtml : " + exc.Message + "/" + exc.StackTrace);
            }
            return strHtml;
        }

        private void ProcessNewsVP(SPList lst)
        {
            string strListe = lst.Title;
            SPQuery spQuery = new SPQuery();
            StringBuilder sb = new StringBuilder();

            string strItemID = ParamFrm("ItemID");

            sb.Append("<Where>");
            if (!string.IsNullOrEmpty(strItemID))
            {
                sb.Append("<Eq><FieldRef Name='ID'/><Value Type='Number'>" + strItemID + "</Value></Eq>");
            }
            sb.Append("</Where>");

            string Query = sb.ToString();
            spQuery.Query = Query;
            spQuery.RowLimit = 1;
            ContextTrace.AddTrace("spQuery.Query : " + spQuery.Query);

            SPListItemCollection items = lst.GetItems(spQuery);
            ContextTrace.AddTrace("items.Count : " + items.Count.ToString());
            foreach (SPListItem item in items)
            {
                ContextTrace.AddTrace("-------------------------------");

                string Categorie = VPLink2013SPHelper.GetValue(item,Fields.CategorieNewsField);
                string Titre = item.GetItemListFieldValueAsText(Fields.TitleField);

                ContextTrace.AddTrace("Categorie : " + Categorie);
                ContextTrace.AddTrace("Titre : " + Titre);

                GetFilAriane(Categorie, Titre);
            }
        }

        private string GetFilAriane(string strCategorie, string strTitre)
        {
            string strLinkAccueil = "/pages/default.aspx";

            sbFilAriane.AppendLine("<div class=\"breadcrumbs\">");
            sbFilAriane.AppendLine("<span><a href=\"" + strLinkAccueil + "\">Accueil</a></span>");
            sbFilAriane.AppendLine("&nbsp;>&nbsp");
            sbFilAriane.AppendLine("<span>News</span>");
            if (!string.IsNullOrEmpty(strCategorie))
            {
                sbFilAriane.AppendLine("&nbsp;>&nbsp");
                sbFilAriane.AppendLine("<span>" + strCategorie + "</span>");
            }
            if (!string.IsNullOrEmpty(strTitre))
            {
                sbFilAriane.AppendLine("&nbsp;>&nbsp");
                sbFilAriane.AppendLine("<span>" + strTitre + "</span>");
            }
            sbFilAriane.AppendLine("</div>");
            return sbFilAriane.ToString();
        }


        #endregion news detail 2013
    }
}
